const innerAudioContext = wx.createInnerAudioContext("myAudio")
Page({
  data: {
    src: ''
  },
  onLoad: function (options) {
    //获取全局唯一的录音管理器 RecorderManager
    innerAudioContext.autoplay = true
    this.recorderManager = wx.getRecorderManager()
    this.recorderManager.onStop((res) => {
      var tempFilePath = res.tempFilePath;// 文件临时路径
      var temp = tempFilePath.replace('.mp3', '')
      console.log(tempFilePath)
      this.data.src=tempFilePath
      wx.uploadFile({
        filePath: tempFilePath,
        name: 'file',
        url: 'http://120.26.163.91:8080/winegame/wine/image/saveImage',
        header:{
          "Content-Type": "multipart/form-data"
        },
        success:function(res){
          console.log(res);
        },
        fail: function(ress){
           console.log("。。录音保存失败。。");
        }
      
      })
    })
    this.recorderManager.onError((res) => {
      console.log('录音失败了！')
    })
  },
  //开始录音
  btnClick1:function(){
    this.recorderManager.start({
      duration: 60000,
      sampleRate: 16000,//采样率，有效值 8000/16000/44100
      numberOfChannels: 1,//录音通道数，有效值 1/2
      encodeBitRate: 96000,//编码码率
      format: 'mp3',//音频格式，有效值 aac/mp3
      frameSize: 50,//指定帧大小
      audioSource: 'auto'//指定录音的音频输入源，可通过 wx.getAvailableAudioSources() 获取
    })
  },
  //录音暂停
  btnClick2: function () {
    this.recorderManager.pause()
  },
  //录音暂停后继续播放
  btnClick3: function () {
    this.recorderManager.resume()
  },
  //录音停止
  btnClick4: function () {
    this.recorderManager.stop()
  },

  audioPlay: function () {
    console.log(this.data.src)
    innerAudioContext.src=this.data.src
    innerAudioContext.onPlay(() => {
      console.log('开始播放')
    })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
  },
  audioPause:function(){
    innerAudioContext.pause()
  }
})
